package im.starchat.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import im.starchat.chat.R;
import im.starchat.chat.activity.StarActivity;
import im.starchat.chat.rest.model.PreviewBean;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.utils.Util;
import im.starchat.chat.view.CircularImageButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.fragment.PreviewFragment;

/**
 * Created by liuchao on 2/10/16.
 * 预告适配器
 */
public class PreviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private static final int TYPE_FEED = 1001;
    private static final int TYPE_EMPTY = 1002;
    private List<PreviewBean> mList;
    private PreviewFragment.OnPreViewCallBack mOnPreViewCallBack;

    public PreviewAdapter(Context context, PreviewFragment.OnPreViewCallBack onPreViewCallBack) {
        this.mContext = context;
        this.mOnPreViewCallBack = onPreViewCallBack;
    }

    public void refreshData(List<PreviewBean> list) {
        this.mList = checkData(list);
        notifyDataSetChanged();
    }

    public void addAll(List<PreviewBean> list) {
        this.mList.addAll(checkData(list));
        notifyDataSetChanged();
    }

    private List<PreviewBean> checkData(List<PreviewBean> list){
        List<PreviewBean> resultList = new ArrayList<>();
        for(PreviewBean pb : list){
            if(!Util.isOutDate(pb.getStart_at())){
                resultList.add(pb);
            }
        }
        return resultList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_EMPTY:
                return new EmptyHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_empty, parent, false));
            case TYPE_FEED:
            default:
                return new PreviewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_preview, parent, false));

        }
    }

    public void changeBookState(int position, boolean state) {
        mList.get(position).setIs_booked(state);
        notifyItemChanged(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_EMPTY:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                emptyHolder.mInfoText.setText("暂无新预告");
                break;
            case TYPE_FEED:
                PreviewHolder paradeHolder = (PreviewHolder) holder;

                paradeHolder.imageButtonUserLogo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!PreferencesStorageUtil.getUserId(mContext).equals(mList.get(position).getUser().getId())) {
                            Intent viewUserInfointent = new Intent();
                            viewUserInfointent.setClass(mContext, StarActivity.class);
                            viewUserInfointent.putExtra(StarActivity.USER_ID, mList.get(position).getUser().getId());
                            mContext.startActivity(viewUserInfointent);
                        }
                    }
                });
                if (!TextUtils.isEmpty(mList.get(position).getUser().getAvatar())) {
                    Picasso.with(mContext).load(mList.get(position).getUser().getAvatar())
                            .placeholder(R.drawable.home_head)
                            .into(paradeHolder.imageButtonUserLogo);
                }
                if (mList.get(position) != null) {
                    paradeHolder.textViewLiveTitle.setText(mList.get(position).getUser().getDescription());
                    if (mList.get(position).getUser() != null) {
                        paradeHolder.textViewUserName.setText(mList.get(position).getUser().getNickname());
                    }
                    paradeHolder.textViewStartTime.setText(Util.formatStringToDateString(mList.get(position).getStart_at()));

                    if (mList.get(position).is_booked()) {
                        paradeHolder.mAppointmentBtn.setText("已预约");
                    } else {
                        paradeHolder.mAppointmentBtn.setText("预约");
                    }

                    paradeHolder.mAppointmentBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mList.get(position).is_booked()) {
                                mOnPreViewCallBack.onDelAppointments(mList.get(position).getId() + "", position);
                            } else {
                                mOnPreViewCallBack.onAppointments(mList.get(position).getId() + "", position);
                            }

                        }
                    });
                    if(!TextUtils.isEmpty(mList.get(position).getDescription())){
                        paradeHolder.mContent.setVisibility(View.VISIBLE);
                        paradeHolder.mContent.setText(mList.get(position).getDescription());
                    }else{
                        paradeHolder.mContent.setVisibility(View.INVISIBLE);
                    }
                }

                if (mList.get(position) != null && !TextUtils.isEmpty(mList.get(position).getCover())) {
                    Picasso.with(mContext).load(mList.get(position).getCover())
                            .placeholder(R.drawable.home_bg)
                            .into(paradeHolder.imageViewCoverImage);
                }

                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (mList == null || mList.size() == 0) {
            return TYPE_EMPTY;
        } else {
            return TYPE_FEED;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 1;
        }
        return mList.size();
    }


    public class PreviewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.image_view_live_cover_image)
        ImageView imageViewCoverImage;
        @InjectView(R.id.image_btn_user_logo)
        CircularImageButton imageButtonUserLogo;
        @InjectView(R.id.text_view_user_name)
        TextView textViewUserName;
        @InjectView(R.id.text_view_live_title)
        TextView textViewLiveTitle;
        @InjectView(R.id.time)
        TextView textViewStartTime;
        @InjectView(R.id.appointment)
        TextView mAppointmentBtn;
        @InjectView(R.id.content)
        TextView mContent;

        public PreviewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    public class EmptyHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.info_text)
        TextView mInfoText;

        public EmptyHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
